﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Timers;

namespace JQGJJ
{
    class SendData
    {
        private static Queue<byte[]> dataQueue = new Queue<byte[]>();
        private static Queue<string> dataQueue_recived = new Queue<string>();
        private static Thread th = null;
        private static Thread th_read = null;
        public static bool stopSate = false;
        private static SerialPort sp = null;
        private static Object lock_ = new Object();

        public static void setSerialPort(SerialPort sp_)
        {
            sp = sp_;
            sp.DataReceived += new SerialDataReceivedEventHandler(threadReadCallBack);
        }
        public static void StopWrite()
        {
            if (th != null && th.ThreadState == System.Threading.ThreadState.Running)
                th.Abort();
            th = null;

        }
        public static void StartWrite()
        {
           th = new System.Threading.Thread(new System.Threading.ThreadStart(threadrCallBack));
            th.Start();
        }
        public static void WriteData(byte[] data)
        {
            if (data.Length <= 0) return;
            dataQueue.Enqueue(data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public static void WriteData(string data)
        {
            lock(lock_)
            {
                var tmp = Encoding.UTF8.GetBytes(data);
                WriteData(tmp);
            }
            
        }
        public static Queue<string> getDataQueue_Recived()
        {
            return dataQueue_recived;
        }
        private static void ParseValidData(string dataStr)
        {


        }
        static void threadReadCallBack(object sender, SerialDataReceivedEventArgs e)
        {
            lock(SerialPortSetDlg._lock)
            {
                if (SerialPortSetDlg.GetSerialPortConnected())
                {
                    SerialPort sp = (SerialPort)sender;
                    string indata = sp.ReadLine();
                    dataQueue_recived.Enqueue(indata);
                }
            }
            
        }
        static void threadrCallBack()
        {
            while (true)
            {
                if (stopSate)
                    continue;

                lock (lock_)
                {
                    if (dataQueue.Count <= 0)
                        continue;
                    var cmd = dataQueue.Dequeue();
                    if (cmd == null)
                        continue;

                    if (sp != null && sp.IsOpen)
                        sp.Write(cmd, 0, cmd.Length);
                }
                Thread.Sleep(50);
            }
        }

        public static void ClearQueue()
        {
            dataQueue.Clear();
        }
    }
}
